package com.example.hellodemo.recyclerView;

import android.content.Context;
import android.content.IntentFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.hellodemo.R;

public class LinearCyclerAdapter extends RecyclerView.Adapter {

    private Context context;
    private OnClickItemListener listener;
    public  LinearCyclerAdapter(Context context,OnClickItemListener listener){
        this.context  = context;
        this.listener = listener;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0){
            return new LinearViewHoler(LayoutInflater.from(context).inflate(R.layout.layout_linear_item,parent,false));

        }{
            return new LinearViewHoler2(LayoutInflater.from(context).inflate(R.layout.layout_linear_item2,parent,false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int type = getItemViewType(position);
        if (type == 0){

            ((LinearViewHoler)holder).textView.setText("可口可乐");


        }else {
            ((LinearViewHoler2)holder).textView.setText("第二种样式");
            Glide.with(context).load("https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=294893614,3986146911&fm=26&gp=0.jpg").into(((LinearViewHoler2)holder).imageView);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return 20;
    }

    // 有多种cell类型 实现该方法 返回几种类型
    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0){
            return  0;
        }else {
            return  1;
        }

    }

    class LinearViewHoler extends RecyclerView.ViewHolder{
        private TextView textView;
        public  LinearViewHoler(View itemView) {
            super(itemView);
            textView =  itemView.findViewById(R.id.linear_lab);
        }
    }

    class LinearViewHoler2 extends RecyclerView.ViewHolder{
        private TextView textView;
        private ImageView imageView;
        public  LinearViewHoler2(View itemView) {
            super(itemView);
            textView =  itemView.findViewById(R.id.linear_lab);
            imageView = itemView.findViewById(R.id.linear_img);
        }
    }

    public interface  OnClickItemListener{
         void onClick(int position);
    }


}
